<?php 

class MemberModel extends BaseModel
{
	private $table;
	
	/**
	 * Hold an instance of the class
	 * @var CommonPageModel
	 */
	private static $_instance = null;
	
	/**
	 * A private constructor
	 * Prevents direct creation of object
	 */
	private function __construct()
	{
		$this->_init();
		$this->table = TABLE_PREFIX . "member";
	}

	/**
	 * The singleton method
	 * @return CommonPageModel
	 */
	public static function getInstance()
	{
		if (!self::$_instance instanceof self)
			self::$_instance = new self;

		return self::$_instance;
	}
	
	public function getDataSource(){
		
		return dibi::dataSource("SELECT * FROM " . $this->table);
	}
	
	public function fetchMembers($cond = array()){
		
		return dibi::select("*")
					->from($this->table)
					->where($cond)
					->fetchAll();
	}
	
	public function authenticateMember($login, $password){
		
		$member = dibi::select("*")
						->from($this->table)
						->where("nick = %s", $login)
						->and("password = %s", sha1($password))
						->fetch();
						
		if(!empty($member)){			
			return $member;
		}
		else return false;
	}
	
	public function addMember($data){
		
		return dibi::insert($this->table, $data)->execute();
	}
	
	public function getPicture($id_file){
		return dibi::fetch("SELECT * FROM " . TABLE_FILES . " WHERE id_file = %i", $id_file);
	}
	
	public function saveMember($data){
		
		if(is_numeric($data["id_member"])){
			
			return dibi::update($this->table, $data)
					->where("id_member = %i", $data["id_member"])
					->execute();
		}else{
			
			return dibi::insert($this->table, $data)
				->execute();
		}
		
		 
	}
	
	public function deleteMember($id_member){
		
		return dibi::delete($this->table)
					->where("id_member = %i", $id_member)
					->execute();
	}
}

?>
